/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.jbfilter.bean.PropertyAccessor;
import org.jbfilter.bean.factory.FilterComponentBeans;
import org.jbfilter.bean.fcomps.delegate.PropertyAccessibleBeanDelegate;
import org.jbfilter.bean.fcomps.single.ContainsStringFilterComponentBean;
import org.jbfilter.bean.fcomps.single.RegexFilterComponentBean;
import org.jbfilter.bean.fcomps.single.SingleFilterComponentBean;
import org.jbfilter.bean.fcomps.single.StringFilterComponentBean;
import org.jbfilter.core.fcomps.single.ContainsStringFilterComponent;
import org.jbfilter.core.fcomps.single.RegexFilterComponent;

class _StringFilterComponentBeanImpl<E>
	extends
		_StringFilterComponentImpl<E>
	implements
		StringFilterComponentBean<E> {

	private PropertyAccessibleBeanDelegate<E, String> pabDelegate;
	
	// constructors
	_StringFilterComponentBeanImpl() {
		super();
		pabDelegate = new PropertyAccessibleBeanDelegate<E, String>();
		pabDelegate.add(getContainsFcTyped());
		pabDelegate.add(getRegexFcTyped());
	}
	_StringFilterComponentBeanImpl(String id) {
		this();
		setId(id);
		setPropertyPath(id);
	}
	_StringFilterComponentBeanImpl(String id, String propertyPath) {
		this();
		setId(id);
		setPropertyPath(propertyPath);
	}
	_StringFilterComponentBeanImpl(String id, PropertyAccessor<E, String> propertyAccessor) {
		this();
		setId(id);
		setPropertyAccessor(propertyAccessor);
	}
	
	@Override
	public void filterOut(LinkedList<E> coll) {
		getActiveFcTyped().filterOut(coll);
	}
	@Override
	public List<E> filter(Collection<E> toFilter) {
		return getActiveFcTyped().filter(toFilter);
	}
	@Override
	public boolean pass(E bean) {
		return getActiveFcTyped().pass(bean);
	}
	@Override
	public String getPropertyValue(E bean) {
		return pabDelegate.getPropertyValue(bean);
	}
	@Override
	public void setPropertyAccessor(PropertyAccessor<E, String> propertyAccessor) {
		pabDelegate.setPropertyAccessor(propertyAccessor);
	}
	@Override
	public String getPropertyPath() {
		return pabDelegate.getPropertyPath();
	}
	@Override
	public void setPropertyPath(String propertyPath) {
		pabDelegate.setPropertyPath(propertyPath);
	}
	private SingleFilterComponentBean<E, String> getActiveFcTyped() {
		return (SingleFilterComponentBean<E, String>)getActiveFc();
	}
	private ContainsStringFilterComponentBean<E> getContainsFcTyped() {
		return (ContainsStringFilterComponentBean<E>)getContainsFc();
	}
	private RegexFilterComponentBean<E> getRegexFcTyped() {
		return (RegexFilterComponentBean<E>)getRegexFc();
	}
	@Override
	protected ContainsStringFilterComponent<E> initContainsFc() {
		return FilterComponentBeans.newContainsStringFilterComponent();
	}
	@Override
	protected RegexFilterComponent<E> initRegexFc() {
		return FilterComponentBeans.newRegexFilterComponent();
	}
}
